<html>
    <head>
        <title>LinkedList</title>
    </head>
    <body>
        Hello LinkedList
        <script>
            //单链表
            function LinkedList(){
                //单链表的节点
                var Node = function(key){
                    this.key = key;
                    this.next = null;
                    // this.pre = null;
                }

                //指针
                var point = null;

                //插入
                this.insert = function(key,position){
                    var newNode = new Node(key);
                    
                    if(point === null){
                        point = newNode;
                    }
                    else if(position!==null){
                        //待完善
                    }                      
                    else{
                        newNode.next = point;
                        point = newNode;
                    }
                }

                //删除 指定位置的值
                this.delete = function(position){
                    if(position===null){
                        console.log(position.key + "被删除");
                        point = point.next;
                    }else{
                        if(position>getLength()-1 || position<0){
                            console.log("此位置不存在");
                        }else{
                            var i=0;
                            var temp = point;
                            while(temp){
                                if(i === position){
                                    //待完善
                                }
                                i++;
                                temp = temp.next;
                            }
                        }
                    }
                }

                //遍历
                this.print = function(){
                    var temp = point;
                    var str = "";
                    while(temp){
                        str += " " + temp.key;
                        temp = temp.next;
                    }
                    console.log(str);
                }

                //长度
                this.getLength = function(){
                    var len = 0;
                    var temp = point;
                    while(temp){
                        len++;
                        temp = temp.next;
                    }
                    return len;
                }    

            }

            //测试
            var nodes = [66,34,98,12,88];
            var list = new LinkedList();
            var len = nodes.length;
            for(var i=0;i<len;i++){
                list.insert(nodes[i],null);
            }
            list.print();                     
            console.log("长度："+list.getLength());
  
        </script>
    </body>
</html>